<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect @getCategory="getCategory" :show="scene != 0" />
    </el-card>
    <el-card>
      <!--底部是有三部分进行切换-->
      <!--展示spu列表-->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-bottom: 10px"
          :disabled="!category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table border style="width: 100%" :data="records">
          <el-table-column align="center" label="序号" type="index" width="80">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作">
            <template slot-scope="{ row }">
              <el-button
                type="success"
                size="mini"
                icon="el-icon-plus"
                title="添加sku"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="warning"
                icon="el-icon-edit"
                title="修改spu"
                size="mini"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看spu列表"
                @click="handlerSkuList(row)"
                style="margin-right: 10px"
              ></el-button>

              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="deleteSpu(row)"
              >
                <el-button
                  slot="reference"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- @size-change="handleSizeChange"
          @current-change="getPageList"-->
        <el-pagination
          style="margin-top: 20px; text-align: center"
          @size-change="handleSizeChange"
          @current-change="getSpuList"
          :current-page="page"
          :page-sizes="[3, 5, 7, 9]"
          :page-size="limit"
          :pager-count="9"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
        >
        </el-pagination>
      </div>
      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spu"
        >添加|修改spu</SpuForm
      >
      <SkuForm v-show="scene == 2" ref="sku" @changeScene="changeScenes"
        >添加sku</SkuForm
      >
    </el-card>
    <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogTableVisible" :before-close="closeMyDialog">
      <el-table border  :data="skuList" v-loading="loading" element-loading-text="拼命加载中" >
        <el-table-column
          property="skuName"
          label="名称"
          width="150"
        ></el-table-column>
        <el-table-column
          property="price"
          label="价格"
          width="150"
        ></el-table-column>
        <el-table-column property="weight" label="重量" width="150"></el-table-column>
        <el-table-column
          label="默认图片" >
          <template slot-scope="{row}">
            <img :src="row.skuDefaultImg" alt="默认图片" style="width:100px;height:100px">
          </template>
        </el-table-column>
      </el-table>
      
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
export default {
  name: "Spu",
  data() {
    //0代表展示spu列表数据 1代表展示spu列表数据 2
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      page: 1,
      limit: 3,
      records: [],
      total: 0,
      //控制三个div谁显示
      scene: 0, //0代表展示spu列表数据 1.添加修改spu 2添加sku
      dialogTableVisible:false, //控制dialog显示与隐藏
      spu:{},
      skuList:[],//存储sku列表的数据
      loading:true,//loading效果
    };
  },
  components: { SpuForm, SkuForm },
  methods: {
    //查看sku按钮的回调
    async handlerSkuList(spu){
      //每次点击保证都有loading
      this.loading = true
      //切换查看清除上次数据
      this.skuList = [] 
      this.dialogTableVisible = true
      this.spu = spu
      //获取sku列表进行展示
      const result = await this.$API.sku.reqFindSku(spu.id)
      console.log(result);
      if(result.code==200){
        this.skuList = result.data
        this.loading = false
      }
    },
    //sku通过父组件修改场景
    changeScenes(scene) {
      this.scene = scene;
    },
    //添加sku
    addSku(row) {
      this.scene = 2;
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },
    //删除spu
    async deleteSpu(row) {
      const result = await this.$API.spu.reqDeleteSpu(row.id);
      if (result.code == 200) {
        this.$message({ type: "success", message: "删除成功" });
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
      }
    },
    //添加spu按钮的回调
    addSpu() {
      this.scene = 1;
      this.$refs.spu.addSpuInIt(this.category3Id);
    },
    //修改spu按钮
    updateSpu(row) {
      this.scene = 1;
      //获取子组件spuForm子组件
      this.$refs.spu.initSpuData(row);
    },
    handleSizeChange(size) {
      this.limit = size;
      this.getSpuList();
    },
    //三级联动的自定义事件
    getCategory({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId;
        //清除二级三级id
        this.category2Id = "";
        this.category3Id = "";
      } else if (level == 2) {
        this.category2Id = categoryId;
        this.category3Id = "";
      } else if (level == 3) {
        this.category3Id = categoryId;
        //获取spu类进行展示
        this.getSpuList();
      }
    },
    //获取spu列表
    async getSpuList(pages = 1) {
      this.page = pages;
      const { page, limit, category3Id } = this;
      const result = await this.$API.spu.reqSpuList(page, limit, category3Id);
      if (result.code == 200) {
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
    //自定义事件回调
    changeScene({ scene, flag }) {
      this.scene = scene;
      if (flag) {
        //再次获取数据停留在当前页面
        this.getSpuList(this.page);
      } else {
        //停留在第一页
        this.getSpuList();
      }
    },
    //关闭对话框之前
    closeMyDialog(done){
      this.loading = true
      this.skuList = []
    }
  },
};
</script>

<style></style>
